package ajobs.websocket.main;

import org.springframework.context.event.EventListener;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

@Component
public class WebSocketEventListener {

    @EventListener
    public void handleDisconnectEvent(SessionDisconnectEvent event) {
        String sessionId = event.getSessionId(); // simpSessionId -> 5wjy51nr
        System.out.println("[连接断开] Session ID: " + sessionId);
        // 可在此处清理用户状态（如从在线列表移除）
    }

    @EventListener
    public void handleConnectEvent(SessionConnectedEvent event) {
        MessageHeaders headers = event.getMessage().getHeaders();
        Object simpSessionId = headers.get("simpSessionId");
        System.out.println("[建立连接] Session ID: " + simpSessionId);
        // 可在此处清理用户状态（如从在线列表移除）
    }
}